// import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getUserinfo} from '@/utils/auth'

const API = axios.create({
  // baseURL: 'http://192.168.1.6:9020/qk'
  // baseURL: 'http://tmp.qk-api.aike.info/qk'
  baseURL: 'http://qk-api.aike.info/qk'
  // baseURL: 'http://192.168.1.2:9020/qk'
  // baseURL: 'http://192.168.1.137:9020/qk' // CEBC </el-select>测试
})

// var config = {
//   method: 'get',
//   url: 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Museum%20of%20Contemporary%20Art%20Australia&inputtype=textquery&fields=formatted_address%2Cname%2Crating%2Copening_hours%2Cgeometry&key=AIzaSyBoD8hX-NJx6fAOMnsOBHfsK8gdS6MkR6Q',
//   headers: { }
// };

// axios(config)
// .then(function (response) {
//   console.log(JSON.stringify(response.data));
// })
// .catch(function (error) {
//   console.log(error);
// });

API.interceptors.request.use(config => {
  NProgress.start()
  config.headers.session_id = getUserinfo()
  return config
})
// 当服务器返回数据经过响应拦截器 调用NProgress.done()
API.interceptors.response.use(config => {
  NProgress.done()
  // console.log(config)
  if (config.data.code === 99) {
    // 99 登录过期
    localStorage.clear()
    setTimeout(() => {
      toLogin()
    }, 1000)
  }
  return config
})

/**
 * 跳转登录页
 * 携带当前页面路由，以期在登录页面完成登录后返回当前页面
 */
const toLogin = () => {
  router.replace({
    path: '/login'
  })
}

export default API
